Option Explicit

Private Const YearIndex   As Byte = 1
Private Const MonthIndex  As Byte = 2
Private Const DayIndex    As Byte = 3
Private Const HourIndex   As Byte = 4
Private Const MinuteIndex As Byte = 5
Private Const SecondIndex As Byte = 6

Private TimeGPS(1 To 6) As Byte
Private Position(1 To 3) As Single
Private Velocity(1 To 3) As Single

Private TimeIsValid As Boolean
Private PositionIsValid As Boolean
Private VelocityIsValid As Boolean

Private Const EastIndex  As Byte = 1
Private Const NorthIndex As Byte = 2
Private Const UpIndex    As Byte = 3
'-------------------------------------------------------------------------------
Public Sub TextDecoder()

' This program decodes GPS data in Garmin Simple Text Output format. The GPS
' data is received over a serial port.

    ' Set this flag to true to display raw text from the GPS receiver.
    Const RawText As Boolean = True

    Call Initialize

    Debug.Print
    Debug.Print "GPS text decoder"
    Delay 0.5
    Debug.Print
    Debug.Print "Waiting for GPS data..."
    Debug.Print

    If (RawText) Then
        Call DisplayRawText
    End If    

    Do
        Call GetFixGPS(TimeGPS, Position, Velocity, TimeIsValid, _
            PositionIsValid, VelocityIsValid)

        Call DisplayData
    Loop

End Sub
'-------------------------------------------------------------------------------
Private Sub DisplayRawText()

' This procedure displays raw text received from the GPS receiver.

    Do
        Debug.Print Chr(GetByte);
    Loop

End Sub
'-------------------------------------------------------------------------------
Private Sub Initialize()

    Dim N As Byte

    TimeIsValid = False
    PositionIsValid = False
    VelocityIsValid = False

    Call InitializeGPS

End Sub
'-------------------------------------------------------------------------------
Private Sub DisplayData()

' This procedure displays decoded GPS data.

    Const Deg As Byte = 176 ' Degree symbol.

    Debug.Print

    Debug.Print " Time:  ";
    If (TimeIsValid) Then
        Call DisplayTime
    Else
        Debug.Print "*"
    End If

    If (PositionIsValid) Then
        Debug.Print "  Lat:  "; CStr(Position(NorthIndex)); Chr(Deg)
        Debug.Print "  Lon:  "; CStr(Position(EastIndex));  Chr(Deg)
        Debug.Print "  Alt:  "; CStr(Position(UpIndex));    " m"
    Else
        Debug.Print "  Lat:  *"
        Debug.Print "  Lon:  *"
        Debug.Print "  Alt:  *"
    End If

    If (VelocityIsValid) Then
        Debug.Print "   Ve:  "; CStr(Velocity(EastIndex));  " m/s"
        Debug.Print "   Vn:  "; CStr(Velocity(NorthIndex)); " m/s"
        Debug.Print "   Vu:  "; CStr(Velocity(UpIndex));    " m/s"
    Else
        Debug.Print "   Ve:  *"
        Debug.Print "   Vn:  *"
        Debug.Print "   Vu:  *"
    End If

End Sub
'-------------------------------------------------------------------------------
Public Sub DisplayTime()

' This procedure displays time in HH:MM:SS format.

    Dim B As Byte
    Dim MSDig As Byte
    Dim LSDig As Byte

    B = TimeGPS(HourIndex)
    LSDig = B Mod 10
    B = B \ 10
    MSDig = B Mod 10
    Debug.Print Chr(MSDig + 48) & Chr(LSDig + 48) & ":";

    B = TimeGPS(MinuteIndex)
    LSDig = B Mod 10
    B = B \ 10
    MSDig = B Mod 10
    Debug.Print Chr(MSDig + 48) & Chr(LSDig + 48) & ":";

    B = TimeGPS(SecondIndex)
    LSDig = B Mod 10
    B = B \ 10
    MSDig = B Mod 10
    Debug.Print Chr(MSDig + 48) & Chr(LSDig + 48)

End Sub
'-------------------------------------------------------------------------------
